/*
 * @Author: JoeKai
 * @Date: 2022-06-04 10:37:08
 * @LastEditTime: 2022-08-25 11:34:32
 * @Description: 
 */
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_base_plugin/base_library/base/base_function.dart';
import 'package:flutter_base_plugin_example/app_manager/app_router_manager.dart';
import 'package:flutter_base_plugin_example/page/cart/vm/cart_view_model.dart';
import '../../app_manager/app_routers.dart';
import '../../config/k_color.dart';
import '../../models/cart_model.dart';

class CartPage extends StatefulWidget {
  const CartPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // ignore: todo
    // TODO: implement createState
    return _cartPageState();
  }
}

// ignore: camel_case_types
class _cartPageState extends BaseProviderState<CartViewModel, CartPage> {
  @override
  void buildCreate() {
    // TODO: implement buildCreate
    setAppBarTitle("购物车");
    hideBackIcon();
    if (Platform.isAndroid) {
      // CommonUtils.showToast("android");
      bottomVertical = 56;
    } else {
      // CommonUtils.showToast("ios");
      bottomVertical = 92;
    }
  }

  @override
  CartViewModel buildViewModel() {
    // TODO: implement buildViewModel
    return CartViewModel();
  }

  @override
  Widget buildWidget(CartViewModel viewModel, BuildContext context) {
    // TODO: implement buildWidget
    return ListView.builder(
      padding: const EdgeInsets.all(0),
      itemCount: viewModel.dataList.length,
      itemBuilder: (context, index) {
        return _buildGoodsListItem(viewModel.dataList[index]);
      },
    );
  }

  @override
  Widget? buildErrorWidget(HttpErrorBean? error) {
    // TODO: implement buildErrorWidget
    return Center(
      child: InkWell(
        onTap: () {
          AppRoutesManager.navigateTo(context, AppRouters.login);
        },
        child: const Text(
          '未登录\n快去登录',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 16,
            color: Color(0xFFADADAD),
          ),
        ),
      ),
    );
  }

  Widget _buildGoodsListItem(CartModel model) {
    return InkWell(
      onTap: () {},
      child: Container(
        padding: EdgeInsets.all(10.h),
        color: KColors.white,
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: 180.h,
                  height: 180.h,
                  child: Image.network(
                    "${model.goodImage}",
                    fit: BoxFit.cover,
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    height: 180.h,
                    margin: EdgeInsets.only(left: 10),
                    // color: Colors.red,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("${model.goodName}",
                            maxLines: 2, overflow: TextOverflow.ellipsis),
                        Row(
                          children: <Widget>[
                            Container(
                              height: 36.h,
                              margin: EdgeInsets.only(right: 10),
                              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),

                              //注意 如果Container里面加上decoration属性，这个时候color属性必须得放在BoxDecoration
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10),
                                color: Color.fromRGBO(230, 230, 230, 0.9),
                              ),

                              child: Text("4g"),
                            ),
                            Container(
                              height: 36.h,
                              margin: EdgeInsets.only(right: 10),
                              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10),
                                color: Color.fromRGBO(230, 230, 230, 0.9),
                              ),
                              child: Text("126"),
                            ),
                          ],
                        ),
                        Text(
                          "¥${model.goodPrice}",
                          style:
                              const TextStyle(color: Colors.red, fontSize: 16),
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
            // Divider(
            //   thickness: 2.h,
            // ),
          ],
        ),
      ),
    );
  }
}
